import axios, { type AxiosRequestHeaders } from 'axios'
import { useTokenStore } from '@/stores/mytoken'
import { refreshToken } from '@/api/users'
import router from '@/router'

const request = axios.create({
  // baseURL: import.meta.env.VITE_API_URL,
})

//请求拦截器
request.interceptors.request.use((config) => {
  if (!config.headers) {
    config.headers = {} as AxiosRequestHeaders
  }
  const store = useTokenStore()
  config.headers.Authorization = store.token
  return config
})

//响应拦截器
request.interceptors.response.use(
  (response) => response,
  async (error) => {
    if (error.response.status === 401) {
      const { data } = await refreshToken()
      if (data.success) {
        //保存新token
        useTokenStore().saveToken(data.content)
        //重新请求之前的接口，并且把结果返回
        return request(error.config)
      } else {
        //跳转到login
        ElMessage.error("刷新token失败，需要重新登录");
        router.push('/login')
        return
      }
    }
    return Promise.reject(error)
  })

export default request